# -*- coding: UTF-8 -*-
"""
@auth:buxiangjie
@date:2022/8/30 14:19
@describe: 测试用例统计
"""

from fastapi import APIRouter
from app.log.ulog import logger_
from app.db.db_api import Statistics
from app.db.conn import DataBasePool
from typing import Dict
from app.item.item import RunCaseDetail, DailyCount

router = APIRouter()


@router.get("/", name="首页")
async def index():
	return {"code": 2000}


@router.post("/runcase_detail", name="上传用例运行信息")
async def runcase_detail(detail: RunCaseDetail):
	sql = f"""
		insert into runcase_detail(batch_id,case_name,status,step,operator,exception_name,run_time,script) values (
		'{detail.batch_id}','{detail.case_name}','{detail.status}','{detail.step}',
		'{detail.operator}','{detail.exception_name}','{detail.run_time}','{detail.script}'
		);
	"""
	await Statistics.runcase_detail(sql, DataBasePool().test_statistics)
	return {'code': 2000}


@router.post("/daily_count", name="获取每日总计数据")
async def daily_detail(daily_count: DailyCount):
	sql = f"""
			select sum(all_num) as all_num,
			sum(pass_num) as pass_num, sum(fail_num) as fail_num, sum(skip_num) as skip_num,
			ROUND(sum(pass_num)/sum(all_num)*100, 2) as pass_rate,
			ROUND(sum(fail_num)/sum(all_num)*100, 2) as fail_rate,
			ROUND(sum(skip_num)/sum(all_num)*100, 2) as skip_rate
			from daily_total_situation where
			script='{daily_count.script}' and 
		"""
	if daily_count.start_date == daily_count.end_date:
		sql = sql + f"run_date='{daily_count.start_date}'"
	else:
		sql = sql + f"'{daily_count.start_date}'<=run_date<='{daily_count.end_date}'"
	sql = sql + ";"
	res = Statistics.exec_select("test", sql, "statistics")
	return res[0]
